package com.lill.common.component.util;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class IpUtil {
	
	protected static Logger logger = LoggerFactory.getLogger(IpUtil.class);
	
	public static String getRequestUserIp(HttpServletRequest request) {
		String ipAddress=null;
		String forwarded=request.getHeader("x-forwarded-for");//10.47.103.13,4.2.2.2,10.96.112.230
		logger.info("---x-forwarded-for:"+forwarded);
		if(StringUtils.isNotEmpty(forwarded)){
			if(forwarded.indexOf(",")>0){
				ipAddress=forwarded.split(",")[0];
			}else{
				ipAddress=forwarded;
			}
		}else{
			String realIp=request.getHeader("X-Real-IP");//10.47.103.13
			logger.info("---X-Real-IP:"+realIp);
			if(StringUtils.isNotEmpty(realIp)){
				ipAddress=realIp;
			}else{
				ipAddress=request.getRemoteAddr();
			}
		}
		return ipAddress;
	}

}
